package main.record;

import base.BaseTableWindow;

import dao.RenovateTableDao;
import exception.NotMatchException;
import javafx.scene.control.TableRow;
import model.Renovate;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

public class RenovateTable extends BaseTableWindow {

    @Override
    protected String setTableTitle() {
        return "重修成绩录入";
    }

    @Override
    protected Object[] setTableColumnNames() {
        return new String[]{"学号", "姓名", "课程", "重修成绩"};
    }

    @Override
    protected List<TableRow> setTableRowDatas() {
//        try {
//            List<Renovate> list = RenovateTableDao.getRenovates();
//            int rowCount = list.size();
//
//            Object[][] objs = new Object[16][4];
//            for (int i = 0; i < rowCount; i++) {
//                Renovate renovate = list.get(i);
//
//                objs[i][0] = renovate.getSno();
//                objs[i][1] = renovate.getSname();
//                objs[i][2] = renovate.getCname();
//                objs[i][3] = renovate.getRevonateScore();
//            }
//            return objs;
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }

        return new ArrayList<>();
    }

    @Override
    protected void onSaveBtnClick() {
        List<TableRow> rows = getAllRows();
        int length = rows.size();

        List<Renovate> list = new ArrayList<>(length);
        int i = 0;
        try {
            while (i < length) {
                TableRow tableRow = rows.get(i);

                String sno = (String) tableRow.getValue(0);
                String sname = (String) tableRow.getValue(1);
                String cname = (String) tableRow.getValue(2);
                String revonateScore = (String) tableRow.getValue(3);
                if (sno == null || sname == null || cname == null || revonateScore == null) {
                    JOptionPane.showMessageDialog(null,
                            "第 " + i + " 行，存在列值未填", "提示", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                Renovate renovate = new Renovate();
                renovate.setSno(sno);
                renovate.setSname(sname);
                renovate.setCname(cname);
                renovate.setRevonateScore(Integer.valueOf(revonateScore));
                list.add(renovate);

                i++;
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null,
                    "第 " + i + " 行，分数格式不正确", "提示", JOptionPane.ERROR_MESSAGE);
            return;
        }

        try {
            RenovateTableDao.saveRenovates(list);
        } catch (NotMatchException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e.getMessage(), "提示", JOptionPane.ERROR_MESSAGE);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }

        setAllRows(new ArrayList<>());
        JOptionPane.showMessageDialog(null, "保存成功", "提示", JOptionPane.INFORMATION_MESSAGE);
    }

}
